//
//  PlayerLockView.swift
//

import UIKit

class PlayerLockView: UIView {

    private var didUnlock: () -> Void = {}
//    private var timer: DispatchSourceTimer?
//    private var tempSecond: CGFloat = 6
    
//    private lazy var progressView: PlayerLockProgressView = {
//        let view = PlayerLockProgressView(frame: CGRect(x: 0, y: 0, width: 100.fit, height: 100.fit))
//        view.isUserInteractionEnabled = false
//        view.isHidden = true
//        return view
//    }()
    
    private lazy var lockImageView: UIImageView = {
        let view = UIImageView()
        view.isUserInteractionEnabled = true
        view.image = "video_btn_longUnlock".image
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.clear
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func show(_ unlock: @escaping () -> Void) {
        let view = PlayerLockView(frame: UIScreen.main.bounds)
        view.didUnlock = unlock
        view.addSubviews()
    }
    
    private func addSubviews() {
        UIApplication.shared.keyWindow?.addSubview(self)
        
        addSubview(lockImageView)
        lockImageView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
        
        let gesture = UILongPressGestureRecognizer(target: self, action: #selector(gestureAction(_:)))
        gesture.minimumPressDuration = 3
        lockImageView.addGestureRecognizer(gesture)
    }
    
    @objc private func gestureAction(_ longPress: UILongPressGestureRecognizer) {
        self.didUnlock()
        self.removeFromSuperview()
//        switch longPress.state {
//        case .ended, .cancelled, .failed:
//            timer?.cancel()
//            timer = nil
//            tempSecond = 6
//            progressView.isHidden = true
//        default:
//            progressView.isHidden = false
//            countDown()
//        }
    }

//    private func countDown() {
//        if timer == nil {
//            let queue = DispatchQueue.global(qos: .default)
//            timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
//            timer?.schedule(wallDeadline: .now(), repeating: 0.01)
//            timer?.setEventHandler {
//                if self.tempSecond <= 1 {
//                    self.timer?.cancel()
//                    self.timer = nil
//                    DispatchQueue.main.async {
//                        self.didUnlock()
//                        self.removeFromSuperview()
//                    }
//                } else {
//                    self.tempSecond -= 0.01
//                }
//                DispatchQueue.main.async {
//                    self.progressView.progress = (5 - (self.tempSecond - 1)) / 5
//                    self.progressView.countLabel.text = "\(Int(self.tempSecond))"
//                }
//            }
//            timer?.resume()
//        }
//    }
}

class PlayerLockProgressView: UIView {
    
    var progress: CGFloat = 0 {
        didSet {
            setNeedsDisplay()
        }
    }
    
    lazy var countLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.textColor = UIColor.white
        view.font = 40.font
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.black.alpha(0.4)
        corner(radii: 50.fit)
        addSubview(countLabel)
        
        countLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        let path = UIBezierPath()
        path.lineWidth = 6.fit
        UIColor.theme.setStroke()
        path.lineCapStyle = .round
        path.lineJoinStyle = .round
        path.addArc(withCenter: CGPoint(x: rect.size.width / 2, y: rect.size.height / 2), radius: 47.fit, startAngle: CGFloat.pi * 1.5, endAngle: CGFloat.pi * 1.5 + CGFloat.pi * 2 * progress, clockwise: true)
        path.stroke()
    }
}
